package com.chinar.funds;

import static com.chinar.funds.until.PercentageParser.parsePercentage;

import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import java.text.ParseException;
import java.util.List;

public class FundPositionAdapter extends RecyclerView.Adapter<FundPositionAdapter.PositionViewHolder> {
    private List<FundPosition> positionList;

    public FundPositionAdapter(List<FundPosition> positionList) {
        this.positionList = positionList;
    }

    @NonNull
    @Override
    public PositionViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_fund_position, parent, false);
        return new PositionViewHolder(view);
    }

    @Override
    public void onBindViewHolder(@NonNull PositionViewHolder holder, int position) {
        FundPosition fundPosition = positionList.get(position);

        holder.tvStockCode.setText(fundPosition.getStockCode());
        holder.tvStockName.setText(fundPosition.getStockName());
        holder.tvLatestPrice.setText(fundPosition.getLatestPrice());
        holder.tvChangePercent.setText(fundPosition.getChangePercent());
        holder.tvHoldingShares.setText(fundPosition.getHoldingShares());
        holder.tvChangeValue.setText(fundPosition.getChangeValue());
        try {
            double number_ChangePercent=parsePercentage(fundPosition.getChangePercent());
            if (number_ChangePercent>0){
                holder.tvChangePercent.setTextColor(0xFFFF0000);
                holder.tvLatestPrice.setTextColor(0xFFFF0000);
<<<<<<< HEAD
                holder.tvChangeValue.setTextColor(0xFFFF0000);
            }else if(number_ChangePercent<0){
                holder.tvChangePercent.setTextColor(0xFF00FF00);
                holder.tvLatestPrice.setTextColor(0xFF00FF00);
                holder.tvChangeValue.setTextColor(0xFF00FF00);
            }else {
                holder.tvChangePercent.setTextColor(0xFF000000);
                holder.tvLatestPrice.setTextColor(0xFF000000);
                holder.tvChangeValue.setTextColor(0xFF000000);
=======
            }else if(number_ChangePercent<0){
                holder.tvChangePercent.setTextColor(0xFF00FF00);
                holder.tvLatestPrice.setTextColor(0xFF00FF00);
            }else {
                holder.tvChangePercent.setTextColor(0xFF000000);
                holder.tvLatestPrice.setTextColor(0xFF000000);
>>>>>>> 7c5005ead232d282f65cd743272b7f2a394cdb17
            }

        } catch (ParseException e) {
            e.printStackTrace();
        }

    }

    @Override
    public int getItemCount() {
        return positionList != null ? positionList.size() : 0;
    }

    static class PositionViewHolder extends RecyclerView.ViewHolder {
        TextView tvStockCode, tvStockName, tvLatestPrice, tvChangePercent, tvHoldingShares, tvChangeValue;

        public PositionViewHolder(@NonNull View itemView) {
            super(itemView);
            tvStockCode = itemView.findViewById(R.id.tv_date);
            tvStockName = itemView.findViewById(R.id.tv_net_value);
            tvLatestPrice = itemView.findViewById(R.id.tv_estimate);
            tvChangePercent = itemView.findViewById(R.id.tv_growth_rate);
            tvHoldingShares = itemView.findViewById(R.id.tv_holding_shares);
            tvChangeValue = itemView.findViewById(R.id.tv_change_value);
        }
    }
}